package com.iconmaker.command;

import com.iconmaker.controller.CanvasActivity;
import com.iconmaker.model.Conf;
import com.iconmaker.model.State;
import com.iconmaker.util.EtcUtil;
import com.iconmaker.view.PixcelView;

public class PenCommand implements ICommand {
	private PixcelView view;
	private int newColor;
	private int oldColor;
	private int x;
	private int y;
	private boolean isValid = true;
	
	public PenCommand(int x, int y, int color) {
		this.newColor = color;
		this.x = x;
		this.y = y;
	}
	
	@Override
	public boolean doCommand() {
		EtcUtil.log("PenCommand");
		EtcUtil.log("x = " + x + " y = " + y);
		
		if((x <= Conf.countOfRowPixcel - 1) && (x >= 0) && (y <= Conf.countOfColumnPixcel - 1) && (y >= 0)) {
			view = CanvasActivity.getContext().getView(x, y);
			
			if(view != null && newColor != State.canvasState[x][y]) {
				this.oldColor = State.canvasState[x][y];
				view.setBackgroundColor(newColor);
				State.canvasState[x][y] = newColor;
				
				return true;
			}
			else {
				isValid = false;
				return false;
			}
		}
		isValid = false;
		return false;
	}
	
	@Override
	public void unDoCommand() {
		if(isValid) {
			view.setBackgroundColor(oldColor);
			State.canvasState[x][y] = oldColor;
		}
	}
}
